//
//  LSZMainController.swift
//  醒浪微博
//
//  Created by 雷顺哲 on 16/7/9.
//  Copyright © 2016年 LSZ. All rights reserved.
//

import UIKit
import SVProgressHUD

class LSZMainController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        let lszTabBar = LSZTabBar()
//        设置控制器为代理
        lszTabBar.lszDelegate = self
//        会出现循环引用因此要用[weak self]
        lszTabBar.composeButtonClosure = { [weak self] in
            print("闭包调用的")
            
//            首先判断是否有登录
            if !LSZUserAccountViewModel.sharedUserAccount.isLogin {
                SVProgressHUD.showErrorWithStatus("请先登录,亲~")
                return
            }
            
            if let weakSelf = self {
                let composeView = LSZComposeView()
                composeView.show(weakSelf)
            }
//            执行到此表示已经登录
//            let composeView = LSZComposeView()
////            获取最上面的window
//            let window = UIApplication.sharedApplication().windows.last
//            
//            
//            window?.addSubview(composeView)
        }
        
        
        setValue(lszTabBar, forKey: "tabBar")
        
        addChildViewController(LSZHomeController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(LSZMessageController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(LSZDiscoverController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(LSZProfileController(), title: "我的", imageName: "tabbar_profile")
        
    }

   
    func addChildViewController(childController: UIViewController, title: String, imageName: String) {
//        设置自定义的tabBarItem
        childController.tabBarItem = LSZTabBarItem()
        
        
//        设置title
        childController.title = title
//        设置图片
        childController.tabBarItem.image = UIImage(named: imageName)
//        修改图片渲染方式
        childController.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
//        修改字体颜色
        childController.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orangeColor()], forState: .Selected)
//        设置字体
        childController.tabBarItem.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFontOfSize(10)], forState: .Normal)
        
//        创建导航控制器
        let nav = UINavigationController(rootViewController: childController)
        addChildViewController(nav)
    }

}



// MARK:实现代理的协议
extension LSZMainController: LSZTabBarDelegate {
    
    func didSelectedComposeButton() {
        print("代理调用的")
    }
}












